##这个脚本用来调试串口
import serial
import array
import os
import signal
from time import sleep
import re 

flag_stop = False

def onsignal_int(a,b):
    print("sigint!")
    global flag_stop
    flag_stop = True

def confir(str):
    for i in range(0,32):
        str=str.replace(chr(i),'')
    return str

signal.signal(signal.SIGINT, onsignal_int)
signal.signal(signal.SIGTERM, onsignal_int)

ser = serial.Serial('COM7', 57600, bytesize=8, parity='E', stopbits=2, timeout=1)
print("serial.isOpen() =",ser.isOpen())


cmd_back = B""
cmd_length = 0x00
cmd_count = 0x00

##s = ser.write(("A20B01000000ADEFA50B01020000CB8D").encode('utf-8'))
curIndex = 0

while True:
    sleep(0.05)
      
    if flag_stop:                # read data until Ctrl+c
        #ser.write(stop)            # send cmd stop before exit
        print("reset cmd has been sent!")
        break
   
    text = ser.read(12)          # read one, with timout
    if len(text) > 0:                    # check if not timeout
        sleep(0.02)
        n = ser.inWaiting()     # look if there is more to read
        if n:
            text = text + ser.read(n) #get it
        cmd_back = cmd_back + text
        text = B""
        
        cmdList = cmd_back.split(B"\xeb\xeb\xeb\xeb\xeb\xeb")
        if(cmd_back == B"\xeb\xeb\xeb\xeb\xeb\xeb\x00\x00\xf1\x01\x10\x01\x00\x13\x00\x02\x00A\x00\x01R\t\x00\x00\x01\x01\x00T\x97\xf8y"):#收到打印任务
            print("==> 接收到打印任务.")
            #eb,eb,eb,eb,eb,eb,00,00,f1,01,00,01,10,0f,00,02,00,2f,00,11,01,00,01,82,1a,1b,55
            s = ser.write(B"\xeb\xeb\xeb\xeb\xeb\xeb\x00\x00\xf1\x01\x00\x01\x10\x0f\x00\x02\x00\x2f\x00\x11\x01\x00\x01\x82\x1a\x1b\x55")
            sleep(0.2)
            #获取打印数据
            s = ser.write(B"\xeb\xeb\xeb\xeb\xeb\xeb\x00\x00\xd0\x01\x00\x01\x10\x15\x00\x01\x00\x9f\x01\x00\x01\x00\xa0\x00\x00\x00\x00\xfe\x07\x01\x4d\xe9\xcb")
            sleep(0.1)
            s = ser.write(B"\xeb\xeb\xeb\xeb\xeb\xeb\x00\x00\xd0\x01\x00\x01\x10\x15\x00\x01\x00\x9f\x01\x00\x01\x00\xa1\x00\x00\x00\x00\xfe\x07\xb5\x46\x9e\x6d")
            sleep(0.1)
            s = ser.write(B"\xeb\xeb\xeb\xeb\xeb\xeb\x00\x00\xd0\x01\x00\x01\x10\x15\x00\x01\x00\x9f\x01\x00\x01\x00\xa2\x00\x00\x00\x00\xfe\x07\x28\x5c\x76\x5c")
            continue
            
        else:
            print("==> %s cmds recvd."%(cmd_back))

        print("-------------")
        cmd_back = B""
    
##    if(cmd_back == B"\xA2\x55\x01\x00\x00\x7A\x8E"):#开机
##       s = ser.write(B"\xA2\x0B\x01\x00\x00\x00\xAD\xEF\xA5\x0B\x01\x02\x00\x00\xCB\x8D")
##       curIndex = 0
##
##    elif(cmd_back == B"\xA2\x55\x00\x00\x55\x47\xEE"): #关机
##        s = ser.write(B"\xA2\x0B\x00\x00\x00\x00\x9A\xDF")
##        curIndex = 0
##                       #0xA3,0x55,0x01,0x00,0x00,0x7A,0x8E
##    elif(cmd_back == B"\xA3\x55\x01\x00\x00\x7A\x8E"): #下一画面
##        curIndex = curIndex + 1
##                
##    elif(cmd_back == B"\xA3\x55\x00\x00\x00\x4D\xBE"): #上一画面
##        curIndex = curIndex - 1
####unsigned char returnStr1[] = {0xA5,0x0B,0x02,0x02,0x00,0x00,0x92,0xDD};
####unsigned char returnStr2[] = {0xA5,0x0B,0x03,0x02,0x00,0x00,0xA5,0xED};
####unsigned char returnStr3[] = {0xA5,0x0B,0x04,0x02,0x00,0x00,0x20,0x7D};
####unsigned char returnStr4[] = {0xA5,0x0B,0x05,0x02,0x00,0x00,0x17,0x4D};
####unsigned char returnStr5[] = {0xA5,0x0B,0x06,0x02,0x00,0x00,0x4E,0x1D};
##    print(curIndex)
##    if(curIndex == 1):
##        s = ser.write(B"\xA5\x0B\x02\x02\x00\x00\x92\xDD")
##    elif(curIndex == 2):
##         s = ser.write(B"\xA5\x0B\x03\x02\x00\x00\xA5\xED")
##    elif(curIndex == 3): 
##         s = ser.write(B"\xA5\x0B\x04\x02\x00\x00\x20\x7D")
##    elif(curIndex == 4):
##         s = ser.write(B"\xA5\x0B\x05\x02\x00\x00\x17\x4D")
##    elif(curIndex == 5): 
##         s = ser.write(B"\xA5\x0B\x06\x02\x00\x00\x4E\x1D")
   
ser.close()
